﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;
using DTO;
using Business;
namespace Airplane
{
    public partial class ThongTinDatVe : System.Web.UI.Page
    {
        int cbd = 0;
        int cbv = 0;
        float tongtien = 0;

        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                 cbd = Convert.ToInt32(Request.QueryString["CBD"]);
                 cbv = Convert.ToInt32(Request.QueryString["CBV"]);
                XuLyLayThongTinChuyenBay ttcb = new XuLyLayThongTinChuyenBay();
                ttcb.chuyenbay.MaChuyenBay = cbd;
                ttcb.ThucThi();
                txtMaChuyenBay.Text = cbd.ToString();
                if(cbd>0)
                txtNgayDi.Text =DateTime.Parse( ttcb.table.Rows[0]["NgayDi"].ToString()).ToShortDateString();
                Session["SoDam"] = ttcb.table.Rows[0]["SoDam"].ToString();
                XuLyLayThongTinChuyenBay ttcbve = new XuLyLayThongTinChuyenBay();
                ttcbve.chuyenbay.MaChuyenBay = cbv;
                ttcbve.ThucThi();
                if (cbv != 0)
                {
                    txtChuyenBayVe.Text = cbv.ToString();
                    txtNgayVe.Text = DateTime.Parse( ttcbve.table.Rows[0]["NgayDi"].ToString()).ToShortDateString();
                     int dam =Convert.ToInt32(ttcb.table.Rows[0]["SoDam"]);
                     Session["SoDam"] = Convert.ToInt32(Session["SoDam"]) + dam;
                }
                txtNguoiLon.Text = (string)Session["NguoiLon"];
                txtTreEm.Text= (string)Session["TreEm"];
                txtEmBe.Text = (string)Session["EmBe"];
                txtHangVe.Text=(string)Session["HangVe"];
            }
        }

        protected void btnDatVe_Click(object sender, EventArgs e)
        {
            XuLyDatVe xldv = new XuLyDatVe();
            xldv.datve.MaChuyenBay = Convert.ToInt32(txtMaChuyenBay.Text);
            xldv.datve.NgayDi = DateTime.Parse(txtNgayDi.Text);
            if (txtChuyenBayVe.Text != "")
            {
                xldv.datve.ChuyenBayVe = Convert.ToInt32(txtChuyenBayVe.Text);
                xldv.datve.NgayVe = DateTime.Parse(txtNgayVe.Text);
            }
            xldv.datve.ThoiGianDatVe = DateTime.Now;

            xldv.datve.SLNgLonPT = 0;
            xldv.datve.SLNgLonTG = 0;
            xldv.datve.SLNgLonTK = 0;
            xldv.datve.SLSoSinhPT = 0;
            xldv.datve.SLSoSinhTG = 0;
            xldv.datve.SLSoSinhTK = 0;
            xldv.datve.SLTreEmPT = 0;
            xldv.datve.SLTreEmTG = 0;
            xldv.datve.SLTreEmTK = 0;
            XuLyLayThongTinChuyenBay cbdi = new XuLyLayThongTinChuyenBay();
            cbdi.chuyenbay.MaChuyenBay = Convert.ToInt32(Request.QueryString["CBD"]);
            cbdi.ThucThi();

            XuLyLayThongTinChuyenBay cbve = new XuLyLayThongTinChuyenBay();
            cbve.chuyenbay.MaChuyenBay = Convert.ToInt32(Request.QueryString["CBV"]);
            cbve.ThucThi();

            if (txtHangVe.Text == "Tiết kiệm" || txtHangVe.Text == "TietKiem" || txtHangVe.Text == "Tiết Kiệm")
            {
                xldv.datve.SLNgLonTK = Convert.ToInt32(txtNguoiLon.Text);
                xldv.datve.SLTreEmTK = Convert.ToInt32(txtTreEm.Text);
                xldv.datve.SLSoSinhTK = Convert.ToInt32(txtEmBe.Text);
                xldv.datve.LoaiVe = 102;
                tongtien += xldv.datve.SLNgLonTK * Convert.ToInt32(cbdi.table.Rows[0]["GiaNgLonTK"]);
                tongtien += xldv.datve.SLTreEmTK * Convert.ToInt32(cbdi.table.Rows[0]["GiaTreEmTK"]);
                tongtien += xldv.datve.SLSoSinhTK * Convert.ToInt32(cbdi.table.Rows[0]["GiaSoSinhTK"]);
                if ((string)Session["LoaiVe"] == "KhuHoi")
                {
                    tongtien += xldv.datve.SLNgLonTK * Convert.ToInt32(cbve.table.Rows[0]["GiaNgLonTK"]);
                    tongtien += xldv.datve.SLTreEmTK * Convert.ToInt32(cbve.table.Rows[0]["GiaTreEmTK"]);
                    tongtien += xldv.datve.SLSoSinhTK * Convert.ToInt32(cbve.table.Rows[0]["GiaSoSinhTK"]);
                }
            }
            if (txtHangVe.Text == "Phổ Thông" || txtHangVe.Text == "PhoThong" || txtHangVe.Text == "Phổ thông")
            {
                xldv.datve.SLNgLonPT = Convert.ToInt32(txtNguoiLon.Text);
                xldv.datve.SLTreEmPT = Convert.ToInt32(txtTreEm.Text);
                xldv.datve.SLSoSinhPT = Convert.ToInt32(txtEmBe.Text);
                xldv.datve.LoaiVe = 101;
                tongtien += xldv.datve.SLNgLonPT * Convert.ToInt32(cbdi.table.Rows[0]["GiaNgLonPT"]);
                tongtien += xldv.datve.SLTreEmPT * Convert.ToInt32(cbdi.table.Rows[0]["GiaTreEmPT"]);
                tongtien += xldv.datve.SLSoSinhPT * Convert.ToInt32(cbdi.table.Rows[0]["GiaSoSinhPT"]);
                if ((string)Session["LoaiVe"] == "KhuHoi")
                {
                    tongtien += xldv.datve.SLNgLonPT * Convert.ToInt32(cbve.table.Rows[0]["GiaNgLonPT"]);
                    tongtien += xldv.datve.SLTreEmPT * Convert.ToInt32(cbve.table.Rows[0]["GiaTreEmPT"]);
                    tongtien += xldv.datve.SLSoSinhPT * Convert.ToInt32(cbve.table.Rows[0]["GiaSoSinhPT"]);
                }

            }
            if (txtHangVe.Text == "Thương Gia" || txtHangVe.Text == "ThuongGia" || txtHangVe.Text == "Thương gia")
            {
                xldv.datve.SLNgLonTG = Convert.ToInt32(txtNguoiLon.Text);
                xldv.datve.SLTreEmTG = Convert.ToInt32(txtTreEm.Text);
                xldv.datve.SLSoSinhTG = Convert.ToInt32(txtEmBe.Text);
                xldv.datve.LoaiVe = 100;
                tongtien += xldv.datve.SLNgLonTG * Convert.ToInt32(cbdi.table.Rows[0]["GiaNgLonTG"]);
                tongtien += xldv.datve.SLTreEmTG * Convert.ToInt32(cbdi.table.Rows[0]["GiaTreEmTG"]);
                tongtien += xldv.datve.SLSoSinhTG * Convert.ToInt32(cbdi.table.Rows[0]["GiaSoSinhTG"]);
                if ((string)Session["LoaiVe"] == "KhuHoi")
                {
                    tongtien += xldv.datve.SLNgLonPT * Convert.ToInt32(cbve.table.Rows[0]["GiaNgLonPT"]);
                    tongtien += xldv.datve.SLTreEmPT * Convert.ToInt32(cbve.table.Rows[0]["GiaTreEmPT"]);
                    tongtien += xldv.datve.SLSoSinhPT * Convert.ToInt32(cbve.table.Rows[0]["GiaSoSinhPT"]);
                }
            }
            xldv.chitiet.SoGiayToTuyThan = txtCMND.Text;
            xldv.chitiet.HoTen = txtHoTen.Text;
            xldv.chitiet.Email = txtEmail.Text;
            xldv.chitiet.SoDT = txtSoDienThoai.Text;
            xldv.chitiet.DiaChi = txtDiaChi.Text;
            xldv.chitiet.SoHoiVien = Convert.ToInt32(txtSoHoiVien.Text);

            xldv.datve.TongTien = tongtien;
            
            try
            {
                int datve= xldv.ThucThi();
                XuLyCapNhatKhachHang capnhatdam = new XuLyCapNhatKhachHang();
                capnhatdam.khachhang.SoDam = Convert.ToInt32(Session["SoDamKhachHang"]) + Convert.ToInt32(Session["SoDam"]);
                capnhatdam.khachhang.SoTheHoiVien = Convert.ToInt32(Session["SoHoiVien"]);
                capnhatdam.CapNhatDamThuong();
                Session["SoDamKhachHang"] = 0; Session["SoDam"] = 0;
                lbDatVe.Text = "Mã đặt vé của quí khách là " + datve.ToString();
                lbTongTien.Text = "Số tiền phải trả là " + string.Format("{0:0,0}", tongtien) + " VND";
                MultiView1.ActiveViewIndex = 1;
                if (datve > 0)
                {
                    lbKetQua.Text = "Quí khách đã đặt vé thành công";
                }
                else
                    lbKetQua.Text = "Đặt vé thất bại";
               
                
            }
            catch(Exception ex)
            {
                throw ex;
            }
        }

        protected void txtSoHoiVien_TextChanged(object sender, EventArgs e)
        {
            XuLyLayThongTinKhachHang ttkh = new XuLyLayThongTinKhachHang();
            ttkh.khachkhang.SoTheHoiVien = Convert.ToInt32(txtSoHoiVien.Text);
            ttkh.LayThongTinHoiVien();
            txtSoCreditCard.Text = ttkh.datatable.Rows[0]["SoCreditCard"].ToString();
            txtSoDienThoai.Text = ttkh.datatable.Rows[0]["DienThoai"].ToString();
            txtCMND.Text = ttkh.datatable.Rows[0]["SoGiayToTuyThan"].ToString();
            txtDiaChi.Text = ttkh.datatable.Rows[0]["DiaChi"].ToString();
            txtEmail.Text = ttkh.datatable.Rows[0]["Email"].ToString();
            txtHoTen.Text = ttkh.datatable.Rows[0]["HoTen"].ToString();
            Session["SoDamKhachHang"] = ttkh.datatable.Rows[0]["SoDam"].ToString();
            Session["SoHoiVien"] = txtSoHoiVien.Text;
        }
       
    }
}